<cfmodule template="../../tag/layout.cfm" bodyIsEasyUILayout="false">
<script type="text/javascript" charset="utf-8" src="ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="ueditor/lang/zh-cn/zh-cn.js"></script>
 <fieldset>
<legend>广告物料清单</legend>
<table id="grid"></table>
<div id="toolbar" style="display: none;">
	<form id="searchForm">
		<table>
			<tr>
				<td>标题</td>
				<td><input name="subject" style="width: 180px;" /></td>
				<td>创建时间</td>
				<td>
					<input name="created_ge"  class="easyui-datebox" style="width:100px"/>
					-
					<input name="created_le"  class="easyui-datebox" style="width:100px"/>
				</td>
				<td>
					<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-zoom',plain:true" onclick="grid.datagrid('load',fw.serializeObject($('#searchForm')));">过滤</a>
					<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-zoom_out',plain:true" onclick="$('#searchForm input').val('');grid.datagrid('load',{});">重置过滤</a>
				</td>
			</tr>
		</table>
	</form>
</div>
</fieldset>

<form method="post" class="form">
	 <fieldset>
    <legend>基本信息</legend>
  <input type="hidden" name="id" />
    <table class="table" id="table" name="table" width="100%">
      <tbody>
        <tr>
          <th>标题</th>
          <td colspan="3"><input id="subject" name="subject" class="easyui-validatebox" data-options="required:true"/></td>
        </tr>
        <tr>
          <th>链接地址</th>
          <td><input class="easyui-validatebox" name="url" style="width:280px;"  data-options="required:true,validType:'url'"/></td>
          <th>弹出方式</th>
          <td>
          	<select name="urltarget">
      			<option value="_self">当前页面</option>
      			<option value="_blank">弹出页面</option>
      		</select>
          </td>
        </tr>
        <tr>
          <th>信息排序</th>
          <td><input name="seq" class="easyui-numberspinner"  value="0"  style="width:50px;" /></td>
          <th>点击次数</th>
          <td><input name="hits" class="easyui-numberspinner" value="0"  style="width:50px;" /></td>
        </tr>

        <tr>
          <th>广告类型</th>
          <td><select name="type" id="type">
      			<option value="0">文本广告</option>
      			<option value="1">图片广告</option>
      		</select></td>
          <th>选择文件</th>
          <td width='240'><select class="selectFiles"><option>选择文件</option></select>
          	<a href="#" class="easyui-linkbutton uploader" data-options="iconCls:'icon-add'">选择文件</a>
          	<input name="pic" class="selectFilesValue" type="hidden" /></td>
        </tr>
        <tr>
          <th>广告内容</th>
          <td colspan="3">
          	<textarea id="content" name="content" style="width:600px; height:120px;"></textarea>
          	<input type='button' mode='0' class='editor' onclick="EditorRender('content',$(this))" value='切换至编辑器'>
          </td>
        </tr>
        <tr>
          <td colspan="4"><div style="padding:5px; text-align:center;"><a href="#" class="easyui-linkbutton" id="submit" data-options="iconCls:'icon-save'"> 确 认 </a></div></td>
        </tr>
      </tbody>
    </table>
  </fieldset>
</form>

</cfmodule>
<script type="text/javascript">

	var closeDialog = function($dialog) {
		$dialog.dialog('destroy');
	}

	//修改操作
	var editFun = function(id) {
		if( $('.editor').attr('mode') == 1 ){
			$('.editor').click();
		}
		$.ajax({
		    type: "POST",
		    url: "?action=adsmaterial.get",
		    data: {id:id}
		}).done(function(result) {
			$('.form').form('load', result);
			//文本型
			if(result.type == 0){
				$('.uploader').prev('.selectFiles').html('<option>选择文件</option>');
				$('.uploader').next('.selectFilesValue').val('');
				$('.uploader').hide();
			}else{
				//图片
				$.ajax({
				    type: "POST",
				    url: "?action=uploader.get",
				    data: {id:result.pic}
				}).done(function(result) {
					$('.uploader').prev('.selectFiles').html('<option>选择文件</option><option>'+ result.clientname+'</option>');
				}, 'json');

				$('.uploader').show();
			}

			parent.$.messager.progress('close');
		}, 'json');
	};

	//删除操作
	var removeFun = function(id) {
	    parent.$.messager.confirm('询问', '您确定要删除此记录？',
	    function(r) {
	        if (r) {
	            $.post('?action=adsmaterial.delete', {
	                id: id
	            },
	            function(result) {
	                grid.datagrid('reload');
	            },
	            'json');
	        }
	    });
	};


	var submitForm = function() {
		if ($('.form').form('validate')) {
			if($('#type').val() == 1 && $('.selectFilesValue').val().trim().length == 0){
				$.messager.alert('提示','请选择广告图片文件！','warning');
			}else{

				var url;
				if ($(':input[name="id"]').val().length > 0) {
					url = '?action=adsmaterial.update';
				} else {
					url = '?action=adsmaterial.save'
				}

				$.post(url, fw.serializeObject($('.form') ), function(result) {
					if (result) {
						grid.datagrid('reload');
//						$('.form').form('clear');
					}
				}, 'json');


			}
		}
	};

	var typeFun = function(){
		if($(this).val() == 1){
			$('.uploader').show();
		}else{
			$('.uploader').prev('.selectFiles').html('<option>选择文件</option>');
			$('.uploader').next('.selectFilesValue').val('');
			$('.uploader').hide();
		}
	}

	$('#submit').on('click',submitForm);
	$('#type').on('change',typeFun);

	$(function(){
		$('.uploader').hide();
		$(".uploader").on('click',uploaderFun);
			//列表操作
		var gird;
		grid = $('#grid').datagrid({
			fit: false,
			border:false,
			collapsible:false,
			pagination:true,
			singleSelect:true,
			rownumbers:true,
			sortName:'id',
			idField:'id',
			sortOrder:'desc',
			toolbar : '#toolbar',
			url:'?action=adsmaterial.list',
			columns:[[
				{field:'id',title:'ID',width:40,sortable:true},
				{field:'type',title:'类型',width:40,sortable:true,align:'center',
				formatter:function(value,row){
					if(value == 0){
						v = '<img src="assets/images/font.png">';
					}else{
						v = '<img src="assets/images/image.png">';
					}
					return v;
				}},
				{field:'subject',title:'标题',width:250,sortable:true},
				{field:'url',title:'链接地址',width:60,sortable:true,
				formatter:function(value,row){
					return '<a href="'+value+'" target="_blank">查看链接</a>'
				}},
				{field:'urltarget',title:'弹出方式',width:60,sortable:true},
				{field:'width',title:'宽度',width:50,sortable:true},
				{field:'height',title:'高度',width:50,sortable:true},
				{field:'seq',title:'排序',width:50,sortable:true},
				{field:'hits',title:'点击',width:50,sortable:true},
				{
					title : '操作',
					field : 'action',
					width : '50',
					formatter : function(value, row) {
						var str = '';
							str += fw.formatString('<img class="iconImg ext-icon-note_edit" title="编辑" onclick="editFun(\'{0}\');"/>', row.id);
							str += fw.formatString(' <img class="iconImg ext-icon-note_delete" title="删除" onclick="removeFun(\'{0}\');"/>', row.id);

						return str;
					}
				}
			]],
			onLoadSuccess : function(data) {
				$('.iconImg').attr('src', fw.pixel_0);
			}
		});


	})
</script>